import { defineStore } from 'pinia'
import {
  fetchPageData,
  getBatchIds,
  reqAddPageData,
  reqBatchBannedByIds,
  reqBatchDeleteData,
  reqBatchPassMerchant,
  reqBatchUntieDisable,
  reqDeleteData,
  reqPassMerchant,
  reqRejectMerchant,
  updatePageInfo,
  uploadImage
} from '@/service/module/page-common'
import { useGlobalStore } from '@/store'

interface IPagination {
  total: number
  current: number
  size: number
}

interface IMerchantUpgradeInfo {
  merchantId: number
  applyStoreCompanyType: number
}

interface IState {
  curRow: any
  pagination: IPagination
  ids: number[]
  merchantUpgradeInfos: IMerchantUpgradeInfo[]
  pageList: any[]
  imgUrl: string
  // userList: any[]
  // roleList: any[]
}

const usePageCommonStore = defineStore('pageCommon', {
  state: (): IState => ({
    // 表格当前操作的行
    curRow: {},
    // 分页相关
    pagination: {
      total: 0,
      size: 10,
      current: 1
    },
    // ids(500条数据)
    ids: [],
    merchantUpgradeInfos: [],
    pageList: [],
    imgUrl: '' // 头像等表单图片
    // 用户管理数据
    // userList: [],
    // 角色管理数据
    // roleList: []
  }),
  actions: {
    // 请求页面数据
    async fetchPageDataAction(pageName: string, info: any) {
      const pageDataRes = await fetchPageData(pageName, info)
      console.log(pageDataRes)
      // 菜单页面的数据获取不是在records，要额外区分
      if (pageName === 'menu' || pageName === 'goodClassify') {
        this.pageList = pageDataRes.data
        return
      }
      this.pagination.total = pageDataRes.data.total
      this.pagination.current = pageDataRes.data.current
      this.pagination.size = pageDataRes.data.size
      this.pageList = pageDataRes.data.records
      // this[`${pageName}List`] = pageDataRes.data.records
    },

    // 获取不同页面的500条数据
    async getBatchIdsAction(
      pageName: string,
      status?: number,
      isMerchant?: boolean
    ) {
      const res = await getBatchIds(pageName, status, isMerchant)
      if (pageName === 'merchantUpgrade' && isMerchant) {
        this.merchantUpgradeInfos = res.data
        return
      }
      this.ids = res.data
    },

    // 批量删除数据
    async reqBatchDeleteDataAction(ids: number[], pageName: string) {
      await reqBatchDeleteData(ids, pageName)
      // 删除成功更新列表数据
      await this.fetchPageDataAction(pageName, { current: 1, size: 10 })
      // 如果删除的是角色数据，要派发一个请求更新存起来的角色数据
      if (pageName === 'role') await useGlobalStore().getRoleListAction()
    },
    // 单操作删除数据
    async reqDeleteDataAction(id: number, pageName: string, skuType: number) {
      await reqDeleteData(id, pageName, skuType)
      // 删除成功更新列表数据
      await this.fetchPageDataAction(pageName, { current: 1, size: 10 })
      // 如果删除的是角色数据，要派发一个请求更新存起来的角色数据
      if (pageName === 'role') await useGlobalStore().getRoleListAction()
      // 如果删除的是菜单数据，要派发一个请求更新存起来的菜单数据
      if (pageName === 'menu') await useGlobalStore().getParentMenuListAction()
    },

    // 批量封禁
    async reqBatchBannedByIdsAction(
      ids: number[],
      bannedTime: number,
      pageName: string
    ) {
      await reqBatchBannedByIds(ids, bannedTime)
      // 封禁成功更新列表数据
      await this.fetchPageDataAction(pageName, { current: 1, size: 10 })
    },

    // 批量解封
    async reqBatchUntieDisableAction(ids: number[], pageName: string) {
      await reqBatchUntieDisable(ids)
      // 解封成功更新列表数据
      await this.fetchPageDataAction(pageName, { current: 1, size: 10 })
    },

    // 更新信息
    async updatePageInfoAction(pageName: string, info: any) {
      await updatePageInfo(pageName, info)
      await this.fetchPageDataAction(pageName, { current: 1, size: 10 })
      // 如果删除的是角色数据，要派发一个请求更新存起来的角色数据
      if (pageName === 'role') await useGlobalStore().getRoleListAction()
      // 如果删除的是菜单数据，要派发一个请求更新存起来的菜单数据
      if (pageName === 'menu') await useGlobalStore().getParentMenuListAction()
    },

    // 新增数据
    async reqAddPageDataAction(pageName: string, info: any) {
      await reqAddPageData(pageName, info)
      await this.fetchPageDataAction(pageName, { current: 1, size: 10 })
      // 如果删除的是角色数据，要派发一个请求更新存起来的角色数据
      if (pageName === 'role') await useGlobalStore().getRoleListAction()
      // 如果删除的是菜单数据，要派发一个请求更新存起来的菜单数据
      if (pageName === 'menu') await useGlobalStore().getParentMenuListAction()
    },

    // 单个通过申请
    async reqPassMerchantAction(pageName, id, applyStoreCompanyType) {
      await reqPassMerchant(pageName, id, applyStoreCompanyType)
      await this.fetchPageDataAction(pageName, { current: 1, size: 10 })
    },

    // 驳回申请
    async reqRejectMerchantAction(pageName: string, info: any) {
      await reqRejectMerchant(pageName, info)
      await this.fetchPageDataAction(pageName, { current: 1, size: 10 })
    },

    // 批量通过
    async reqBatchPassMerchantAction(pageName: string, ids: number[]) {
      await reqBatchPassMerchant(pageName, ids)
      await this.fetchPageDataAction(pageName, { current: 1, size: 10 })
    },

    // 上传图片
    async uploadImageAction(pageName: string, file: any) {
      const res = await uploadImage(pageName, file)
      this.imgUrl = import.meta.env.VITE_BASE_URL + res.data
    }
  }
})

export default usePageCommonStore
